package com.qcxy.gzy.activity;

import java.util.Timer;

import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;

import com.qcxy.gzy.activity.v2.TotalActivity;
import com.qcxy.gzy.activity.v2.TrainingActivity;
import com.qxcy.gzy.util.SPUtils;

/**
 * 次数界面 <br>
 *
 * @author gzy
 */
public class NumberActivity extends TotalActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mTargetNumber.setText(R.string.target_number);
        mUnit.setText(R.string.number_uint);
        mSeekBar.setProgress(Integer.parseInt(SPUtils.getValueFromSharedFile(this, "TargetNumber", 0 + "")));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_go: {
                SPUtils.setValueSharedFile(this, "TargetNumber", mTarget + "");
                Intent intent = new Intent(NumberActivity.this,
                        TrainingActivity.class);
                intent.putExtra("Target", mTarget + "");
                intent.putExtra("Activity", "Number");
                startActivity(intent);
                NumberActivity.this.finish();
                break;
            }

            case R.id.iv_back: {
                NumberActivity.this.finish();
                break;
            }

            case R.id.iv_target_reduce: {
                if (mTarget > 0) {
                    mTarget--;
                    mTargetValue.setText(String.valueOf(mTarget));
                    mSeekBar.setProgress(mTarget);
                }
                break;
            }

            case R.id.iv_target_plus: {
                if (mTarget < 100) {
                    mTarget++;
                    mTargetValue.setText(String.valueOf(mTarget));
                    mSeekBar.setProgress(mTarget);
                }
                break;
            }

            default:
                break;
        }
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // 只要当down返回true时候，系统将不把本次事件记录为点击事件，也就不会触发onClick或者onLongClick事件了
                timer = new Timer(true);
                switch (v.getId()) {
                    case R.id.iv_target_reduce:
                        timer.schedule(new TimeTask(1), 1000, 100);
                        break;

                    case R.id.iv_target_plus:
                        timer.schedule(new TimeTask(2), 1000, 100);
                        break;
                }
                break;

            case MotionEvent.ACTION_UP:
                timer.cancel();
                timer.purge();
                timer = null;
                break;

            case MotionEvent.ACTION_CANCEL:
                timer.cancel();
                timer.purge();
                timer = null;
                break;
        }
        return false;
    }
}
